package com.fortune.controller.app;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.MapUtils;
import com.fortune.dal.dao.BankCardDAO;
import com.fortune.dal.dao.BankDAO;
import com.fortune.dal.dao.TradeQueryDAO;
import com.fortune.dal.model.Bank;
import com.fortune.dal.model.BankCard;
import com.fortune.dal.model.Cust;
import com.fortune.dal.model.TradeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tradequery")
public class AppTradeQueryController {

    @Autowired
    private TradeQueryDAO tradeQueryDAO;

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private BankCardDAO bankCardDAO;

    @Autowired
    private BankDAO bankDAO;

    @RequestMapping(value = "alltradequery", method = RequestMethod.GET)
    public ModelAndView indexOfAllOne() throws BusinessException {
        ModelAndView mv = new ModelAndView("alltradequery");
        mv.addObject("menuFlag", "alltradequery");
        return mv;
    }

    @RequestMapping(value = "accounttrade", method = RequestMethod.GET)
    public ModelAndView indexOfAllTwo() throws BusinessException {
        ModelAndView mv = new ModelAndView("accounttrade");

        List<Map<String,Object>> list = new ArrayList<>();
        try {
            List<TradeQuery> lists = tradeQueryDAO.queryByModel(new TradeQuery());
            if(ListUtils.isEmpty(lists)){
                mv.addObject("message", "暂无交易记录");
            }

            Cust user = (Cust)httpSession.getAttribute("user");

            for(TradeQuery tradeQuery : lists){
                if(user.getId().equals(tradeQuery.getCustId())){
                    Map<String,Object> tradeMap = MapUtils.parseMap(tradeQuery);
                    if(tradeQuery.getTradeOperator().equals("取现")){
                        tradeMap.put("iconUrl","../images/icon-11.png");
                    }else if(tradeQuery.getTradeOperator().equals("充值")){
                        tradeMap.put("iconUrl","../images/icon-12.png");
                    }else{
                        tradeMap.put("iconUrl","../images/icon-13.png");
                    }

                    list.add(tradeMap);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mv.addObject("list", list);
        mv.addObject("menuFlag", "tradequery");
        return mv;
    }

    @RequestMapping(value = "producttrade", method = RequestMethod.GET)
    public ModelAndView indexOfAllThree() throws BusinessException {
        ModelAndView mv = new ModelAndView("producttrade");
        mv.addObject("menuFlag", "producttrade");
        return mv;
    }


    @RequestMapping(value = "productreturn", method = RequestMethod.GET)
    public ModelAndView indexOfAllFour() throws BusinessException {
        ModelAndView mv = new ModelAndView("productreturn");
        mv.addObject("menuFlag", "productreturn");
        return mv;
    }

    @RequestMapping(value = "tradedetails", method = RequestMethod.GET)
    public ModelAndView indexOfAllFive(@RequestParam Integer queryId) throws BusinessException {
        ModelAndView mv = null;
        TradeQuery param = new TradeQuery();
        param.setId(queryId);
        try {
            TradeQuery tradeQuery = tradeQueryDAO.queryById(param.getId());
            Map<String,Object> map = MapUtils.parseMap(tradeQuery);
            BankCard bankCard = bankCardDAO.queryById(tradeQuery.getBankCardId());
            String cardCode = bankCard.getCardCode();
            Bank bank = bankDAO.queryById(bankCard.getBankId());
            String bankName = bank.getName();
            String simpleCode = cardCode.substring(cardCode.length() - 4);

            StringBuilder sb = new StringBuilder();
            sb.append(bankName);
            sb.append(simpleCode);
            map.put("tradeWay",sb);

            if(tradeQuery.getTradeOperator().equals("取现")){
                map.put("tradeOperator","快速取现");
                mv = new ModelAndView("cashdetails");

            }else if(tradeQuery.getTradeOperator().equals("充值")){

                mv = new ModelAndView("rechargedetails");
            }

            mv.addObject("map", map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mv;
    }
}
